//
//  BBBlobDetector.h
//  BBVImageTest
//
//  Created by ben smith on 5/27/08.
//  This file is part of BBTouch.
//
//  BBTouch is free software: you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.

//  BBTouch is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Lesser General Public License for more details.

//  You should have received a copy of the GNU Lesser General Public License
//  along with BBTouch.  If not, see <http://www.gnu.org/licenses/>.
// 
//  Copyright 2008 Ben Britten Smith ben@benbritten.com .
//

#import <Cocoa/Cocoa.h>

// this is based on the algo described by:
//A Linear-Time Component-Labeling Algorithm 
//Using Contour Tracing Technique 
//Fu Chang, Chun-Jen Chen, and Chi-Jen Lu 
//Institute of Information Science, Academia Sinica 
//128 Academia Road, Section 2, Nankang, Taipei 115 Taiwan 
//E-mail: fchang@iis.sinica.edu.tw, dean@iis.sinica.edu.tw, cjlu@iis.sinica.edu.tw 
// originally found here:
// http://www.iis.sinica.edu.tw/~fchang/paper/component_labeling_cviu.pdf  
// also many thanks to Pawel Solyga for his work on the openTouch project which is what got
// this all started for me


@interface BBBlobDetector : NSObject {
	NSMutableArray * blobs;
	NSMutableArray * filteredBlobs;
	int pixelsHigh,pixelsWide,samplesPerPixel,bytesPerRow;
	unsigned char *srcBytes;
	unsigned char *postBytes;
	int * labels;
	int neighborGrid[8];
	int xGrid[8];
	int yGrid[8];
	int threshold;
	
	NSRect roi;
	int yMin;
	int yMax;
	int xMax;
	int xMin;
	
	NSImage * bgImage;
	unsigned char *bgBytes;
	
	BOOL loopPixelIsValid;
	int loopX,loopY;
	int loopXYDataPosition;
	int contourX,contourY;
	int contourXYDataPosition;
	int contourNeighborCheckIndex;
	int contourLabel;
	int labelIndex;
	
	int maxDataPosition;
	int debugMax;
	
	BOOL invert;
}

@property (assign) BOOL invert;

- (BOOL)contour;
- (BOOL)externalContour;
- (BOOL)internalContour;
- (BOOL)validBlobPixel:(int)position;
- (BOOL)validContourPixel;
- (BOOL)validLoopPixel;
- (BOOL)validLoopPixelAbove;
- (BOOL)validLoopPixelBelow;
- (BOOL)validNeighborPixel;
- (NSArray*)blobs;
- (NSImage*)bgImage;
- (id) init;
- (int)findNextNeighbor;
- (int)threshold;
- (void) dealloc;
- (void)analyze:(NSImage*)anImage;
- (void)analyzeRawData ;
- (void)clearStateArray;
- (void)filter;
- (void)handleGoodPixel;
- (void)moveContourStateToNeighbor:(int)neighborIndex;
- (void)printBlobs;
- (void)scanSrcDataWithROI;
- (void)setBgImage:(NSImage*)anImage;
- (void)setROI:(NSRect)aRect;
- (void)setThreshold:(int)aThresh;
- (void)setupNeighborGrid;
- (void)setupStateArrays;

// 28 methods



@end
